package com.kintreda.ieco_server.util.page;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class PageKit {
	public static String getUrl(String url,int page){
		if(url.contains("&") || url.contains("?")){
			url += "&page=" + page;
		}else{
			url += "?page=" + page;
		}
		return url;
	}
	public static String build(String url,PageBean<? extends Serializable> page){
		if(StringUtils.isBlank(url) || page == null || page.getAllCount()==0 ){
			return "";
		}else{
			StringBuilder builder = new StringBuilder();
			builder.append("<div class=\"pager\">\r\n");
			builder.append("	<ul>\r\n");
			List<Integer> pages = page.getPages();
			if(pages == null || pages.size() == 0){
				return "";
			}
			for(int i = 0; i < pages.size();i++){
				if(pages.get(i) != null){
					if(pages.get(i).intValue() == page.getPage()){
						builder.append("			<li class=\"current\"><a href=\"javascript:goPage(1)\">").append(pages.get(i).intValue()).append("</a></li>\r\n");
					}else{
						builder.append("			<li><a href=\"javascript:goPage(");
						builder.append(pages.get(i));
						builder.append(")\">").append(pages.get(i).intValue()).append("</a></li>\r\n");
					}
				}
			}
			//只有一页屏蔽输入框
			if(page.getAllPage() == 1){
				builder.append("			<li class=\"index\">第&nbsp;").append(page.getPage()).append("&nbsp;页</li>\r\n");
				builder.append("			<li class=\"option\">\r\n");
				builder.append("				<div>\r\n");
				builder.append("					<span>").append(page.getRows()).append("条/页</span>\r\n");
				builder.append("					<span>共").append(page.getAllPage()).append("条</span>\r\n");
				builder.append("				</div>\r\n");
				builder.append("			</li>\r\n");
				buildPageSize(builder,page);
			}else{
				builder.append("			<li class=\"index\">第&nbsp;<input type=\"text\" class=\"num\" value=\"").append(page.getPage()).append("\"/>&nbsp;页</li>\r\n");
				builder.append("			<li class=\"go\"><input type=\"button\" class=\"icon-pager icon-pager-go\"/></li>\r\n");
				buildPageSize(builder,page);
			}
			builder.append("		</ul>\r\n");
			builder.append("	</div>\r\n");
			return builder.toString();
		}
	}
	
	public static void buildPageSize(StringBuilder builder , PageBean<? extends Serializable> page){
		builder.append("			<li class=\"option\">\r\n");
		builder.append("				<div>\r\n");
		builder.append("					<select id=\"pagesize-combox\" name=\"pagesize-combox\">\r\n");
		if(page.getRows() == 15){
			builder.append("						<option value=\"15\" selected='selected'>15</option>\r\n");
			builder.append("						<option value=\"20\">20</option>\r\n");
			builder.append("						<option value=\"50\">50</option>\r\n");
			builder.append("						<option value=\"100\">100</option>\r\n");
		}else if(page.getRows() == 20){
			builder.append("						<option value=\"15\">15</option>\r\n");
			builder.append("						<option value=\"20\" selected='selected'>20</option>\r\n");
			builder.append("						<option value=\"50\">50</option>\r\n");
			builder.append("						<option value=\"100\">100</option>\r\n");
		}else if(page.getRows() == 50){
			builder.append("						<option value=\"15\">15</option>\r\n");
			builder.append("						<option value=\"20\">20</option>\r\n");
			builder.append("						<option value=\"50\" selected='selected'>50</option>\r\n");
			builder.append("						<option value=\"100\">100</option>\r\n");
		}else if(page.getRows() == 100){
			builder.append("						<option value=\"15\">15</option>\r\n");
			builder.append("						<option value=\"20\">20</option>\r\n");
			builder.append("						<option value=\"50\">50</option>\r\n");
			builder.append("						<option value=\"100\" selected='selected'>100</option>\r\n");
		}
		builder.append("					</select>\r\n");
		builder.append("					<span>条/页,</span>\r\n");
		builder.append("					<span>共");
		builder.append(page.getAllCount()).append("条</span>,<span class='total-page-count'>").append(page.getAllPage()).append("页</span>\r\n");
		builder.append("				</div>\r\n");
		builder.append("			</li>\r\n");
	}
	
}
